package com.yichenyang.ordersystem.controller;


import com.yichenyang.ordersystem.entity.Chart;
import com.yichenyang.ordersystem.entity.Statistic;
import com.yichenyang.ordersystem.service.AdminService;
import com.yichenyang.ordersystem.service.CustomerService;
import com.yichenyang.ordersystem.service.OrdermasterService;
import com.yichenyang.ordersystem.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;


@Controller
@RequestMapping("index")
public class IndexController {

    @Autowired
    AdminService adminService;
    @Autowired
    CustomerService customerService;
    @Autowired
    OrdermasterService ordermasterService;
    @Autowired
    ProductService productService;

    @GetMapping("statistic")
    public String index(HttpSession session){
        Statistic statistic = new Statistic();
        statistic.setMemberNum(customerService.queryAll());
        statistic.setAdminNum(adminService.queryAll());
        statistic.setProductNum(productService.queryAll());
        statistic.setOrderNum(ordermasterService.queryAll());
        statistic.setMemberTodayNum(customerService.queryByConditions("",getTime("yyyy-MM-dd"),"").size());
        statistic.setAdminTodayNum(adminService.queryByConditions("",getTime("yyyy-MM-dd"),"").size());
        statistic.setProductTodayNum(productService.queryByConditions("",getTime("yyyy-MM-dd"),"").size());
        statistic.setOrderTodayNum(ordermasterService.queryByConditions(null,null,null,getTime("yyyy-MM-dd"),"").size());
        statistic.setMemberYesNum(customerService.queryByConditions("",getYesTime(),getTime("yyyy-MM-dd")).size());
        statistic.setAdminYesNum(adminService.queryByConditions("",getYesTime(),getTime("yyyy-MM-dd")).size());
        statistic.setProductYesNum(productService.queryByConditions("",getYesTime(),getTime("yyyy-MM-dd")).size());
        statistic.setOrderYesNum(ordermasterService.queryByConditions(null,null,null,getYesTime(),getTime("yyyy-MM-dd")).size());
        statistic.setMemberWeekNum(customerService.queryByConditions("",getWeekTime(),"").size());
        statistic.setAdminWeekNum(adminService.queryByConditions("",getWeekTime(),"").size());
        statistic.setProductWeekNum(productService.queryByConditions("",getWeekTime(),"").size());
        statistic.setOrderWeekNum(ordermasterService.queryByConditions(null,null,null,getWeekTime(),"").size());
        statistic.setMemberMouthNum(customerService.queryByConditions("",getTime("yyyy-MM")+"-01","").size());
        statistic.setAdminMouthNum(adminService.queryByConditions("",getTime("yyyy-MM")+"-01","").size());
        statistic.setProductMouthNum(productService.queryByConditions("",getTime("yyyy-MM")+"-01","").size());
        statistic.setOrderMouthNum(ordermasterService.queryByConditions(null,null,null,getTime("yyyy-MM")+"-01","").size());
        session.setAttribute("sta",statistic);
        return "pages/welcome::statistic";
    }

    @GetMapping("chart")
    @ResponseBody
    public Chart getChart() {
        Chart chart = new Chart();
        String date = "";
        String user = "";
        String admin = "";
        String product = "";
        String order = "";
        for (int i = 6; i >= 0; i--) {
            date += getBeforeTime(i);
            user += customerService.queryByConditions("",getBeforeTime(i),getBeforeTime(i-1)).size();
            admin += adminService.queryByConditions("",getBeforeTime(i),getBeforeTime(i-1)).size();
            product += productService.queryByConditions("",getBeforeTime(i),getBeforeTime(i-1)).size();
            order += ordermasterService.queryByConditions(null,null,null,getBeforeTime(i),getBeforeTime(i-1)).size();
            if (i != 0) {
                date += ",";
                user += ",";
                admin += ",";
                product += ",";
                order += ",";
            }
        }
        chart.setDate(date);
        chart.setUser(user);
        chart.setAdmin(admin);
        chart.setProduct(product);
        chart.setOrder(order);
        return chart;
    }

    @GetMapping("all")
    @ResponseBody
    public String getAll() {
        String all = "";
        all += customerService.queryAllByLimit(0,customerService.queryAll()).size() + ",";
        all += adminService.queryAllByLimit(0,adminService.queryAll()).size() + ",";
        all += productService.queryAllByLimit(0,productService.queryAll()).size() + ",";
        all += ordermasterService.queryAllByLimit(0,ordermasterService.queryAll()).size();
        return all;
    }

    public String getTime(String pattern) {
        Date date = new Date();
        DateFormat dateFormat = new SimpleDateFormat(pattern);
        String now = dateFormat.format(date);
        return now;
    }

    public String getYesTime() {
        Date date = new Date();
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Calendar yes = Calendar.getInstance();
        yes.setTime(date);
        yes.add(Calendar.DAY_OF_MONTH, -1);
        return dateFormat.format(yes.getTime());
    }

    public String getWeekTime() {
        Date date = new Date();
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        // 使用Calendar类进行时间的计算
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int dayWeek = cal.get(Calendar.DAY_OF_WEEK);
        if (dayWeek == 1) {
            dayWeek = 7;
        } else {
            dayWeek -= 1;
        }
        cal.add(Calendar.DAY_OF_MONTH, 1 - dayWeek);
        Date startDate = cal.getTime();
        return dateFormat.format(startDate);
    }

    public String getBeforeTime(int amount) {
        Date date = new Date();
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Calendar yes = Calendar.getInstance();
        yes.setTime(date);
        yes.add(Calendar.DAY_OF_MONTH, -amount);
        return dateFormat.format(yes.getTime());
    }


}
